import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

//引入组织架构图插件
import vue3TreeOrg from 'vue3-tree-org';
import "vue3-tree-org/lib/vue3-tree-org.css";

//1.引入element-plus组件库
import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'

//quill富文本编辑器
// import Quill from "vue3-quill-editor";
// import "vue3-quill-editor/lib/style.css";
// import Quill from "vue3-quill-editor";
// import "vue3-quill-editor/lib/style.css";
//
import VideoPlayer from 'vue-video-player'
import 'vue-video-player/src/custom-theme.css'
import 'video.js/dist/video-js.css'


//3.引入element-plus图标库
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

//5.1修改elementPlus默认中文
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import axios from "axios";

//引入echarts图表库
import * as echarts from 'echarts';

//8.1配置根路径
const BASE_URL = 'http://localhost:8080'
//window里配置的都是全局内容,可以在任意script标签内部使用
window.BASE_URL = BASE_URL;

const app = createApp(App)
//给app也做全局根路径配置,方便在template标签中使用
app.config.globalProperties.BASE_URL = BASE_URL;

//4.遍历ElementPlusIconVue图标库,将其中所有的图标组件[key,component]依次注册到app中
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//2.use(ElementPlus)    使用element-plus组件库
//5.2 app.use(ElementPlus,{ locale:zhCn })
app.use(store).use(router).use(ElementPlus, {locale: zhCn}).use(echarts).use(vue3TreeOrg).mount('#app')

//6.解决ResizeObserver Error
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}
//7.配置获取当前登录用户的全局方法
window.getUser = () => {
    //如果获取到了用户数据,转回JS对象
    //如果获取不到用户数据,Boolean('')是false,得到null
    return localStorage.user ? JSON.parse(localStorage.user) : null;
}

//9.创建全局方法,用来获取指定code对应的字典项
window.loadDictOptions = (object, dictCode) => {
//向后端发请求获取字典项
    axios.get(BASE_URL + '/v1/dictoption/select/' + dictCode).then((response) => {
        if (2000 === response.data.code) {
            //将字典项保存到对象中
            object.value = response.data.data;
        } else {
            ElMessage.error(response.data.msg);
        }
    })
}